/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "game_fuc.h"
#include "game_flow.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
typedef StaticTask_t osStaticThreadDef_t;
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
extern Weapon_t *usersweapon;//玩家武器
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for UsersbulletTask */
osThreadId_t UsersbulletTaskHandle;
uint32_t UsersbulletTaskBuffer[ 256 ];
osStaticThreadDef_t UsersbulletTaskControlBlock;
const osThreadAttr_t UsersbulletTask_attributes = {
  .name = "UsersbulletTask",
  .cb_mem = &UsersbulletTaskControlBlock,
  .cb_size = sizeof(UsersbulletTaskControlBlock),
  .stack_mem = &UsersbulletTaskBuffer[0],
  .stack_size = sizeof(UsersbulletTaskBuffer),
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for UpdateframeTask */
osThreadId_t UpdateframeTaskHandle;
uint32_t UpdateframeTaskBuffer[ 256 ];
osStaticThreadDef_t UpdateframeTaskControlBlock;
const osThreadAttr_t UpdateframeTask_attributes = {
  .name = "UpdateframeTask",
  .cb_mem = &UpdateframeTaskControlBlock,
  .cb_size = sizeof(UpdateframeTaskControlBlock),
  .stack_mem = &UpdateframeTaskBuffer[0],
  .stack_size = sizeof(UpdateframeTaskBuffer),
  .priority = (osPriority_t) osPriorityBelowNormal4,
};
/* Definitions for EnemiesbulTask */
osThreadId_t EnemiesbulTaskHandle;
uint32_t EnemiesbulTaskBuffer[ 256 ];
osStaticThreadDef_t EnemiesbulTaskControlBlock;
const osThreadAttr_t EnemiesbulTask_attributes = {
  .name = "EnemiesbulTask",
  .cb_mem = &EnemiesbulTaskControlBlock,
  .cb_size = sizeof(EnemiesbulTaskControlBlock),
  .stack_mem = &EnemiesbulTaskBuffer[0],
  .stack_size = sizeof(EnemiesbulTaskBuffer),
  .priority = (osPriority_t) osPriorityNormal1,
};
/* Definitions for BossChangeTask */
osThreadId_t BossChangeTaskHandle;
uint32_t BossChangeTaskBuffer[ 256 ];
osStaticThreadDef_t BossChangeTaskControlBlock;
const osThreadAttr_t BossChangeTask_attributes = {
  .name = "BossChangeTask",
  .cb_mem = &BossChangeTaskControlBlock,
  .cb_size = sizeof(BossChangeTaskControlBlock),
  .stack_mem = &BossChangeTaskBuffer[0],
  .stack_size = sizeof(BossChangeTaskBuffer),
  .priority = (osPriority_t) osPriorityNormal2,
};
/* Definitions for TrackBulletTask */
osThreadId_t TrackBulletTaskHandle;
uint32_t TrackBulletTaskBuffer[ 256 ];
osStaticThreadDef_t TrackBulletTaskControlBlock;
const osThreadAttr_t TrackBulletTask_attributes = {
  .name = "TrackBulletTask",
  .cb_mem = &TrackBulletTaskControlBlock,
  .cb_size = sizeof(TrackBulletTaskControlBlock),
  .stack_mem = &TrackBulletTaskBuffer[0],
  .stack_size = sizeof(TrackBulletTaskBuffer),
  .priority = (osPriority_t) osPriorityBelowNormal3,
};
/* Definitions for UsersbulletQueue */
osMessageQueueId_t UsersbulletQueueHandle;
const osMessageQueueAttr_t UsersbulletQueue_attributes = {
  .name = "UsersbulletQueue"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void usersbulletTask(void *argument);
void updateframeTask(void *argument);
void enemiesbulTask(void *argument);
void bossChangeTask(void *argument);
void trackBulletTask(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of UsersbulletQueue */
  UsersbulletQueueHandle = osMessageQueueNew (1, sizeof(uint16_t), &UsersbulletQueue_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of UsersbulletTask */
  UsersbulletTaskHandle = osThreadNew(usersbulletTask, NULL, &UsersbulletTask_attributes);

  /* creation of UpdateframeTask */
  UpdateframeTaskHandle = osThreadNew(updateframeTask, NULL, &UpdateframeTask_attributes);

  /* creation of EnemiesbulTask */
  EnemiesbulTaskHandle = osThreadNew(enemiesbulTask, NULL, &EnemiesbulTask_attributes);

  /* creation of BossChangeTask */
  BossChangeTaskHandle = osThreadNew(bossChangeTask, NULL, &BossChangeTask_attributes);

  /* creation of TrackBulletTask */
  TrackBulletTaskHandle = osThreadNew(trackBulletTask, NULL, &TrackBulletTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_usersbulletTask */
/**
  * @brief  Function implementing the UsersbulletTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_usersbulletTask */
void usersbulletTask(void *argument)
{
  /* USER CODE BEGIN usersbulletTask */
  uint8_t bulletType = 0;
  /* Infinite loop */
  for(;;)
  {
    if(gameFlow == gameLevel)
    {
      osMessageQueueGet(UsersbulletQueueHandle, &bulletType, NULL, osWaitForever);
      if(bulletType)
      {
        usersBullet_Create(bulletType);
        bulletType = 0;
      }
    }
    osDelay(usersweapon->wGenTime);
  }
  /* USER CODE END usersbulletTask */
}

/* USER CODE BEGIN Header_updateframeTask */
/**
* @brief Function implementing the UpdateframeTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_updateframeTask */
void updateframeTask(void *argument)
{
  /* USER CODE BEGIN updateframeTask */
  
  /* Infinite loop */
  for(;;)
  {    
    /*               手柄操作             */
    JoyStick_GetData();
    
    /*               游戏界面切换             */
    gameInterfaceSwitch(&gameFlow);
    
    osDelay(33);//刷新频率30Hz
  }
  /* USER CODE END updateframeTask */
}

/* USER CODE BEGIN Header_enemiesbulTask */
/**
* @brief Function implementing the EnemiesbulTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_enemiesbulTask */
void enemiesbulTask(void *argument)
{
  /* USER CODE BEGIN enemiesbulTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1000);
    if(gameFlow == gameLevel)
    {
      enemiesBullet_Create();
      BossBulletChat_Create();
      BossBullet1_Create();
      osDelay(200);
      BossBullet1_Create();
      osDelay(200);
      BossBullet1_Create();
      osDelay(200);
      BossFire_Create();
    }
  }
  /* USER CODE END enemiesbulTask */
}

/* USER CODE BEGIN Header_bossChangeTask */
/**
* @brief Function implementing the BossChangeTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_bossChangeTask */
void bossChangeTask(void *argument)
{
  /* USER CODE BEGIN bossChangeTask */
  /* Infinite loop */
  for(;;)
  {
    enemiesTrack();
    Boss_weak();
    osDelay(33);
    Boss_strong();
  }
  /* USER CODE END bossChangeTask */
}

/* USER CODE BEGIN Header_trackBulletTask */
/**
* @brief Function implementing the TrackBulletTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_trackBulletTask */
void trackBulletTask(void *argument)
{
  /* USER CODE BEGIN trackBulletTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(3000);
    enemiesTrackBullet_Create();
  }
  /* USER CODE END trackBulletTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

